home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / comm / astr201.zip / ANSISTRP.BAS next >
BASIC Source File  |  1991-02-26  |  5KB  |  141 lines

  1. ' ANSISTRP.BAS by Duane Paulson  Version 2.00
  2. ' Simple utility to strip ansi escape sequences from a text file.
  3.  
  4. ' For Microsoft BASIC Professional Developer's System, version 7.1.
  5. ' Sorry, some functions are not compatible with QuickBASIC.
  6.  
  7. ' BASIC Professional Developer's System and QuickBASIC are trademarks of Microsoft Corporation.
  8.  
  9. ' Compiler instructions:
  10. '     8088 version:
  11. '        BC /E /O /Ot /Fpa /C:1 ansistrp.bas, astr.obj,;
  12. '     80286 version:
  13. '        BC /E /O /Ot /Fpa /C:1 /G2 ansistrp.bas, astr286.obj,;
  14.  
  15. ' Linker instructions:
  16. '     LINK /NOE astr[286].obj+tscnionr.obj+smallerr.obj+nolpt.obj+nocom.obj+noedit.obj,astr[286].exe,,BCL71ANR.LIB,;
  17.  
  18. DEFINT A-Z
  19.  
  20. CONST Title$ = "ANSISTRP Version 2.01 ANSI Strip Utility by Duane Paulson 02/26/91"
  21. CONST CString$ = "Portions (C) 1982-1990 Microsoft Corporation. All rights reserved."
  22.  
  23. CONST AnsiEsc$ = ""            ' This is the ANSI escape to be looked for
  24.  
  25. CONST InAnsi$ = "0123456789;"    ' Any of these characters following an ANSI
  26.                                  ' escape indicate that we are still in the
  27.                                  ' ANSI control code.
  28.  
  29. OPEN "CONS:" FOR OUTPUT AS #3             ' Print to STDOUT so nobody's
  30. PRINT #3,                                 ' custom screen setup gets wrecked
  31.  
  32. Cl$ = COMMAND$
  33. IF Cl$ = "" THEN PRINT #3, CHR$(7); : GOTO Help  'Need 2 Command line parameters
  34.  
  35. ParamEnd = INSTR(Cl$, " ")          ' Find the space between the two params
  36. IF ParamEnd = 0 THEN GOTO Help
  37. InFile$ = LEFT$(Cl$, ParamEnd - 1)  ' Parse the Command parameters
  38. OutFile$ = MID$(Cl$, ParamEnd + 1)
  39.  
  40. IF DIR$(InFile$) = "" THEN GOTO NotFound     ' The parameters should be
  41. ON ERROR GOTO ErrTrp                         ' valid filenames
  42.  
  43. OPEN OutFile$ FOR OUTPUT AS #2 LEN = 20480   ' Check and see
  44.  
  45. ON ERROR GOTO 0                              ' Turn it back off
  46.  
  47.  
  48. OPEN InFile$ FOR INPUT AS #1 LEN = 20480     ' Open the source
  49.  
  50. PRINT #3, "Working";
  51.  
  52. DO WHILE NOT EOF(1)                          ' Main loop
  53.  
  54.    LINE INPUT #1, a$                         ' Grab a line
  55.  
  56.    StartAnsi = INSTR(a$, AnsiEsc$)           ' Any ANSI?
  57.    StrLen = LEN(a$)
  58.  
  59.    DO WHILE StartAnsi                 ' If ANSI found, this is the strip loop
  60.  
  61.       ' Compare the characters in CONST InAnsi$ to the current line on a
  62.       ' character by character basis. A soon as we hit on a character that
  63.       ' is not an the middle of ANSI, we jump out, with k set on the ANSI
  64.       ' terminator.
  65.  
  66.       FOR k = StartAnsi + 2 TO StrLen
  67.          IF INSTR(InAnsi$, MID$(a$, k, 1)) = 0 THEN EXIT FOR
  68.       NEXT k
  69.  
  70.       left = StartAnsi - 1     ' Now we need to know if the ANSI escape is
  71.                                ' at the start of the line or not
  72.  
  73.       IF left THEN             ' ... and process accordingly.
  74.          a$ = MID$(a$, 1, left) + MID$(a$, k + 1)
  75.       ELSE
  76.          a$ = MID$(a$, k + 1)
  77.       END IF
  78.  
  79.       StartAnsi = INSTR(a$, AnsiEsc$)   ' Find out if there's any more ANSI
  80.       StrLen = LEN(a$)                  ' and adjust the length
  81.  
  82.    LOOP                                 ' End of the strip loop
  83.  
  84.    PRINT #2, a$                         ' Print stripped material to file
  85.    PRINT #3, ".";                       ' Let user know it's working
  86.  
  87. LOOP                                    ' And grab the next line.
  88.  
  89. CLOSE #1, #2                            ' All done!
  90. PRINT #3,
  91. GOTO Ender
  92.  
  93. ErrTrp:
  94.    PRINT #3, CHR$(7); "Unable to open "; OutFile$; " as Output File. Reason:"
  95.    SELECT CASE ERR
  96.       CASE 25
  97.          e$ = "Device Fault"
  98.       CASE 61
  99.          e$ = "Disk Full"
  100.       CASE 64
  101.          e$ = "Bad Filename"
  102.       CASE 68
  103.          e$ = "Device Unavailable"
  104.       CASE 70
  105.          e$ = "Permission Denied (Read Only or Write Protect)"
  106.       CASE 71
  107.          e$ = "Disk Not Ready (Check Drive Door)"
  108.       CASE 72
  109.          e$ = "Disk Media Error"
  110.       CASE 75
  111.          e$ = "Path/File Access Error"
  112.       CASE 76
  113.          e$ = "Path Not Found"
  114.       CASE ELSE
  115.          e$ = "Unknown Type Error"
  116.    END SELECT
  117.    PRINT #3, e$
  118.    PRINT #3,
  119.    El = 2
  120.    GOTO Help
  121.  
  122. NotFound:
  123.    PRINT #3, CHR$(7); "Input file "; InFile$; " Not Found"
  124.    PRINT #3,
  125.    El = 1
  126. Help:
  127.    PRINT #3, "Syntax: astr[286] input_filename output_filename"
  128.    PRINT #3, "  Both parameters are required."
  129.    PRINT #3, "  A space must seperate each parameter."
  130.    PRINT #3,
  131.  
  132. Ender:
  133.    PRINT #3, Title
  134.    PRINT #3, CString
  135.    PRINT #3,
  136.  
  137.    CLOSE
  138.  
  139. END El
  140.  
  141.